package com.utbm.lo54.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.utbm.lo54.dao.SessionDAOInterface;
import com.utbm.lo54.entity.Session;

public class MysqlSessionDAO implements SessionDAOInterface{

	private DataSource dataSource;

	public MysqlSessionDAO(){
		try {
			Context namingContext = new InitialContext();
			dataSource = (DataSource) namingContext.lookup("java:comp/env/jdbc/testDatasource");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public Session create(Session se) {
		Connection connection = null;
		try {
			connection = dataSource.getConnection();
			PreparedStatement stmt = (PreparedStatement) connection.prepareStatement("INSERT INTO COURSE.SESSION(CODE_COURSE,ID_LOCATION,START_SESSION,END_SESSION) VALUES (?,?,?,?)", new String[] { "ID_SESSION" });
			stmt.setString(1, se.getCourseCode());
			stmt.setLong(2, se.getLocationId());
			stmt.setDate(3,  new java.sql.Date(se.getStartDate().getTime()));
			stmt.setDate(4, new java.sql.Date(se.getEndDate().getTime()));
			stmt.executeUpdate();
			ResultSet rs = stmt.getGeneratedKeys();
			if (rs.next()) {
				se.setId(rs.getLong(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}
		return se;
	}



	@Override
	public Session findById(Long id) {
		Connection connection = null;
		Session se = new Session();
		try {
			connection = dataSource.getConnection();
			PreparedStatement stmt = (PreparedStatement) connection.prepareStatement("Select CODE_COURSE, ID_LOCATION, START_SESSION, END_SESSION FROM COURSE.SESSION WHERE ID_SESSION=?");
			stmt.setLong(1, id);

			ResultSet set = stmt.executeQuery();
			while(set.next()){
				
				se.setCourseCode(set.getString(1));
				se.setLocationId(set.getLong(2));
				se.setStartDate(set.getDate(3));
				se.setEndDate(set.getDate(4));
				se.setId(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}
		return se;
	}

	@Override
	public Session findByDate(Date begin, Date end) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(Session se) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(Session se) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public java.util.Set<Session> getAllSession() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Set<Session> findByCourse(String code) {
		Connection connection = null;
		Set<Session> list = new HashSet<Session>();
		try {
			connection = dataSource.getConnection();
			PreparedStatement stmt = (PreparedStatement) connection.prepareStatement("Select ID_SESSION, ID_LOCATION, START_SESSION, END_SESSION FROM COURSE.SESSION WHERE CODE_COURSE=?");
			stmt.setString(1, code);

			ResultSet set = stmt.executeQuery();
			while(set.next()){
				Session se = new Session();
				se.setId(set.getLong(1));
				se.setLocationId(set.getLong(2));
				se.setStartDate(set.getDate(3));
				se.setEndDate(set.getDate(4));
				se.setCourseCode(code);
				list.add(se);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}
		return list;
	}

	

}
